package cn.itcast.bos.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.domain.transit.InOutStorageInfo;
import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.repository.InOutStorageInfoRepository;
import cn.itcast.bos.repository.TransitInfoRepository;
import cn.itcast.bos.service.InOutStorageInfoService;

@Service
@Transactional
public class InOutStorageInfoServiceImpl implements InOutStorageInfoService {

	@Autowired
	private InOutStorageInfoRepository inOutStorageInfoRepository;
	
	@Autowired
	private TransitInfoRepository transitInfoRepository;

	@Override
	public void save(InOutStorageInfo inOutStorageInfo, String transitInfoId) {
		//保存出入库信息
		inOutStorageInfoRepository.save(inOutStorageInfo);
		//将出入库信息和运输信息关联
		//根据运输单号的id查询运输单信息
		TransitInfo transitInfo = transitInfoRepository.findOne(Integer.parseInt(transitInfoId));
		//关联出入库信息 到 运输配送对象，生成数据列的集合索引，@OrderColumn(name = "C_IN_OUT_INDEX")
		transitInfo.getInOutStorageInfos().add(inOutStorageInfo);
		//当出入库表单选择到达网点时，修改运输单的状态为到达网点
		if(inOutStorageInfo.getOperation().equals("到达网点")) {
			transitInfo.setStatus("到达网点");
			//更新运输单的网点地址
			transitInfo.setOutletAddress(inOutStorageInfo.getAddress());
		}
	}

}
